package com.alibaba.pet.service.impl;

import com.alibaba.basic.service.IBaiduAiAuditService;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.util.AjaxResult;
import com.alibaba.basic.util.BaiduAuditUtils;
import com.alibaba.pet.domain.Pet;
import com.alibaba.pet.domain.PetDetail;
import com.alibaba.pet.domain.PetOnlineAuditLog;
import com.alibaba.pet.mapper.PetDetailMapper;
import com.alibaba.pet.mapper.PetMapper;
import com.alibaba.pet.mapper.PetOnlineAuditLogMapper;
import com.alibaba.pet.service.IPetService;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Override
    public void add(Pet pet) {
        // 添加宠物信息
        super.add(pet);
        // 添加宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);

        }
    }

    @Override
    public void delete(Long id) {
        // 删除宠物信息
        super.delete(id);
        petDetailMapper.deleteByPetId(id);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail!=null){
            petDetailMapper.update(petDetail);

        }
    }

    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);

        }
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        petMapper.offdale(ids,new Date());
        return new AjaxResult();
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.findById(id);
            // 审核类容和图像
            Boolean aBoolean = baiduAiAuditService.textAudit(pet.getName());
            Boolean aBoolean1 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
           log.setPetId(id);
            if (aBoolean &&  aBoolean1){
                // 该状态
                petMapper.onsale(id,new Date());
                // 添加审核日志
                log.setState(1);
                log.setNote("审核成功");
                // 没有审核人 百度智能审核
                petOnlineAuditLogMapper.add(log);

            }else {
                // 添加审核日志
                log.setState(0);
                log.setNote("审核失败");
                // 没有审核人 百度智能审核
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMessage("上架失败");

            }

        }

        return new AjaxResult();
    }
}
